package com.by.yz.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.DemoHelper;
import com.by.yz.common.MFGT;
import com.by.yz.mypersonal.RevisePassword;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * Created by 123 on 2016/10/7.
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rp;
    private RelativeLayout phone;
    private RelativeLayout shengming;
    private RelativeLayout copyright;
    private RelativeLayout about;
    private Button logout;
    String myPhone="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_setting_activity);
        setToolbarTitle("设置与帮助");
        myPhone = getIntent().getStringExtra("myPhone");
        Log.e("myPhone0 = ",myPhone);
        initView();
        initDatas();

    }

    private void initDatas() {

    }

    private void initView() {
        rp=(RelativeLayout) findViewById(R.id.setting_rp);
        rp.setOnClickListener(this);
        phone=(RelativeLayout) findViewById(R.id.setting_phone);
        phone.setOnClickListener(this);
        shengming=(RelativeLayout) findViewById(R.id.setting_shengming);
        shengming.setOnClickListener(this);
        copyright=(RelativeLayout) findViewById(R.id.pc_check_update);
        copyright.setOnClickListener(this);
        about=(RelativeLayout) findViewById(R.id.about);
        about.setOnClickListener(this);
        logout=(Button) findViewById(R.id.logout);
        logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.setting_rp:
//                MFGT.startActivity(this, RevisePassword.class);
                Intent intent0 = new Intent(this, RevisePassword.class);
                intent0.putExtra("myPhone", myPhone);
                startActivity(intent0);
                break;

            case R.id.setting_phone:
                MFGT.startActivity(this, com.by.yz.mypersonal.PersonalPhoneActivity.class);
                break;

            case R.id.setting_shengming:
                MFGT.startActivity(this, com.by.yz.mypersonal.PersonalShengmingActivity.class);
                break;

            case R.id.pc_check_update:
                MFGT.startActivity(this, com.by.yz.mypersonal.PersonalCopyrightActivity.class);
                break;

            case R.id.about:
                MFGT.startActivity(this, com.by.yz.mypersonal.PersonalAboutActivity.class);
                break;

            case R.id.logout:
                MainActivity.mainActivity.finish();
                final ProgressDialog pd = new ProgressDialog(SettingActivity.this);
                String st = getResources().getString(R.string.Are_logged_out);
                pd.setMessage(st);
                pd.setCanceledOnTouchOutside(false);
                pd.show();
                EMClient.getInstance().logout(true,new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        SettingActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                pd.dismiss();
                            }
                        });
                    }
                    @Override
                    public void onProgress(int progress, String status) {
                    }
                    @Override
                    public void onError(int code, String message) {
                        SettingActivity.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                pd.dismiss();
                                Toast.makeText(SettingActivity.this, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                setResult(RESULT_OK);
                //清除登陆缓存token
                App.clearCacheUserToken(SettingActivity.this);
                Intent intent = new Intent(this, LoginActivity.class);
                intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                this.finish();
                break;
        }
    }

}
